<template>
  <div class="saveUpdate">
    <el-dialog :close-on-click-modal="false" :title="id?'修改总代':'新增总代'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="500px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="130px" size="mini">
        <dialog-wrapper :MaxWidth="400">
          <el-form-item label="代理账号："
                        prop="user_name" :rules="[{ required: true, message: '代理名称不能为空'}]">
            <el-input v-model="DetailInfo.user_name" :disabled="!!id" placeholder="请输入总代名称..."></el-input>
          </el-form-item>

          <el-form-item label="关联商户：" prop="mch_ids" :rules="[{ required: true, message: '商户不能为空'}]">
            <el-select clearable v-model="DetailInfo.mch_ids" multiple placeholder="请选择">
              <el-option v-for="item in MerchantsList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="IP白名单：">
            <el-input v-model="white_ips" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="安全码：">
            <el-switch v-model="DetailInfo.use_vcode"></el-switch>
          </el-form-item>
          <el-form-item label="代理状态：">
            <el-switch v-model="DetailInfo.enable"></el-switch>
          </el-form-item>
          <el-form-item label="备注：">
            <el-input v-model="DetailInfo.remark"></el-input>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import {getAgencyDetail, saveAgency, updateAgency} from '@/api'
  import {useVcode} from "../../utils/CommonJs";

  export default {
    components: {DialogWrapper},
    data() {
      return {
        setbank: false,
        showDialog: this.showUpdate,
        loading: false,
        white_ips: '',
        DetailInfo: {
          prologue: '亲，您好，下方按钮是目前可用充值方式，请点选您需要的充值方式',
          mch_ids: [],
          enable: true,
          use_vcode: false,
          white_ips: ''
        }
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      id: {
        type: Number
      },
      AgencyType: {
        type: Object
      },
      MerchantsList: {
        type: Array
      },
    },
    created() {
      if (this.id) {
        this.getDetail()
      }
    },
    methods: {
      saveDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            useVcode(this, () => {
              let list = [];
              if (this.white_ips.trim() != "") {
                list = this.white_ips.split(',')
              }
              this.DetailInfo.white_ips = list;
              saveAgency(this.DetailInfo).then(res => {
                if (res.status === 0) {
                  this.$message({
                    message: `代理${res.data.nickname}账号创建成功, 密码为: ${res.data.password}`,
                    type: 'success',
                    duration: 3000, showClose: true
                  })
                  this.$emit('update:showUpdate', false)
                }
              })
            })
          }
        })
      },
      updateDetail() {
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            useVcode(this, () => {
              let list = [];
              if (this.white_ips.trim() != "") {
                list = this.white_ips.split(',')
              }
              this.DetailInfo.white_ips = list;
              updateAgency(this.DetailInfo, this.id).then(res => {
                if (res.status === 0) {
                  this.$message({message: '编辑成功。', type: 'success'})
                  this.$emit('update:showUpdate', false)
                }
              })
            })
          }
        })
      },
      getDetail() {
        getAgencyDetail(this.id).then(res => {
          if (res.status === 0) {
            let {user_name, use_vcode, enable, nickname, mchs, prologue, min_amount, max_amount, warning_amount, remark, white_ips} = res.data
            let mchsId = []
            mchs.forEach(function (item) {
              mchsId.push(item.id)
            })
            if (white_ips != null) {
              this.white_ips = white_ips.toString();
            }
            this.DetailInfo = {
              user_name,
              use_vcode,
              enable,
              nickname,
              mch_ids: mchsId || [],
              prologue,
              min_amount,
              max_amount,
              warning_amount,
              remark,
            }

          }
        })
      }
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>
